﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Namespace Arwani
    Partial Class Backend_ManageExhibitionCategory
        Inherits System.Web.UI.Page
         
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then

                If IsPostBack Then Exit Sub
                Call loadData()
                If CInt(Request.QueryString("catid")) <> 0 Then Call loadEditData(CInt(Request.QueryString("catid")))
            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadEditData(ByVal Id As Integer)
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from  exhibitions_category  where  catid=" & Id
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("catname")) Then Me.txtCategoryName.Text = dtTable.Rows(0)("catname")
                If Not IsDBNull(dtTable.Rows(0)("sortorder")) Then Me.txtSortOrder.Text = dtTable.Rows(0)("sortorder")

            End If

        End Sub
        Public Sub loadData()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from exhibitions_category"
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dgCats.DataSource = dtTable
            Me.dgCats.DataBind()

        End Sub

        Protected Sub dgCats_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgCats.ItemCommand
            If e.CommandName = "Edit" Then
                Response.Redirect("ManageExhibitionCategory.aspx?catid=" & CInt(e.CommandArgument))
            ElseIf e.CommandName = "Delete" Then
                Dim arwCon As New ArwaniController
                Dim strQ As String = "delete from exhibitions_category where catid=" & CInt(e.CommandArgument)
                arwCon.ExecuteNonQuery(strQ)
                Call loadData()
            End If
        End Sub

        Protected Sub dgCats_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCats.ItemDataBound
            Try
                Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
                lnkDelete.Attributes.Add("onclick", "return confirm('Confirm delete this category?')")

                Dim catID As Integer = dgCats.DataKeys(e.Item.ItemIndex)
                Dim txtSortOrder As TextBox = CType(e.Item.FindControl("txtSortOrder"), TextBox)
                txtSortOrder.Attributes.Add("onblur", "postSortOrder(this.value,'exhibitions_category', 'catID','" & catID & "')")
            Catch ex As Exception

            End Try
        End Sub
        

         
        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            If Me.txtCategoryName.Text = "" Then Me.lblError.Text = "Please enter category name" : Exit Sub
            Dim arwCon As New ArwaniController
            Dim strQ As String
            If CInt(Request.QueryString("catid")) = 0 Then
                strQ = "insert into  exhibitions_category (catname,sortorder) values ('" & Me.txtCategoryName.Text & "'," & Me.txtSortOrder.Text & ")"
                arwCon.ExecuteNonQuery(strQ)
            Else 
                strQ = "update exhibitions_category set catname='" & Me.txtCategoryName.Text & "',sortorder=" & Me.txtSortOrder.Text & " where catid=" & CInt(Request.QueryString("catid"))
                arwCon.ExecuteNonQuery(strQ)
            End If

            Response.Redirect("ManageExhibitionCategory.aspx")
        End Sub

        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageExhibition.aspx")
        End Sub
    End Class
End Namespace